<template>
    <el-container>
      <el-header>用户管理</el-header>
      <el-main>
        <div class="addbtn">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-plus"
            plain
            @click="showAddinput"
            >新增姓名</el-button
          >
          <!-- 搜索 -->
          <el-input
            placeholder="请输入性别"
            v-model="select_word"
            size="small"
            style="width: 230px; margin-left: 5px"
            prefix-icon="el-icon-search"
          >
          </el-input>
        </div>
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="name" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="age" label="年龄" width="120"> </el-table-column>
          <el-table-column prop="sex" label="性别" width="120"> </el-table-column>
          <el-table-column prop="phone" label="联系电话" width="400">
          </el-table-column>
          <el-table-column prop="address" label="详细地址"  width="900" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="操作" >
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- <div style="margin-top: 20px">
      <el-button @click="toggleSelection([tableData[1], tableData[2]])">切换第二、第三行的选中状态</el-button>
      <el-button @click="toggleSelection()">取消选择</el-button>
    </div> -->
        <el-button
          class="addbtn"
          type="danger"
          style="margin-top: 10px"
          size="small"
          :disabled="this.multipleSelection.length == 0"
          @click="batchDelete"
        >
          批量删除
        </el-button>
        <el-dialog title="新增用户" :visible.sync="dialogVisible" width="30%">
          <div class="inp">
            姓名
            <el-input
              style="width: 200px; margin-left: 5px"
              size="small"
              class="adduser"
              placeholder="请输入姓名"
              v-model="addData.name"
            >
            </el-input>
          </div>
          <div class="inp">
            年龄
            <el-input
              style="width: 200px; margin-left: 5px"
              size="small"
              class="adduser"
              placeholder="请输入年龄"
              v-model="addData.age"
            >
            </el-input>
          </div>
          <div class="inp">
            性别
            <el-input
              style="width: 200px; margin-left: 5px"
              size="small"
              class="adduser"
              placeholder="请输入姓别"
              v-model="addData.sex"
            >
            </el-input>
          </div>
          <div class="inp">
            联系电话
            <el-input
              style="width: 200px; margin-left: 5px"
              size="small"
              class="adduser"
              placeholder="请输入联系电话"
              v-model="addData.phone"
            >
            </el-input>
          </div>
          <div class="inp">
            详细地址
            <el-input
              style="width: 200px; margin-left: 5px"
              size="small"
              class="adduser"
              placeholder="请输入地址"
              v-model="addData.address"
            >
            </el-input>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="adduser">确 定</el-button>
          </span>
        </el-dialog>
        <!-- 编辑框 -->
        <el-dialog title="编辑用户" :visible.sync="dialogFormVisible">
          <el-form :model="editUser">
            <el-form-item label="姓名">
              <el-input v-model="editUser.name" style="width: 70%"></el-input>
            </el-form-item>
            <el-form-item label="年龄">
              <el-input v-model="editUser.age" style="width: 70%"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-input v-model="editUser.sex" style="width: 70%"></el-input>
            </el-form-item>
            <el-form-item label="电话">
              <el-input v-model="editUser.phone" style="width: 70%"></el-input>
            </el-form-item>
            <el-form-item label="地址">
              <el-input v-model="editUser.address" style="width: 70%"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateuser">确 定</el-button>
          </div>
        </el-dialog>
      </el-main>
    </el-container>
  </template>
  
  <script>
  export default {
    name: "user",
    data() {
      return {
        tableData: [
          {
            name: "王小虎",
            age: "18",
            sex: "男",
            phone: "13111112222",
            address: "上海市普陀区金沙江路 1518 弄",
          },
          {
            name: "王小虎",
            age: "18",
            sex: "女",
            phone: "13111112222",
            address: "上海市普陀区金沙江路 1518 弄",
          },
          {
            name: "王小虎",
            age: "18",
            sex: "男",
            phone: "13111112222",
            address: "上海市普陀区金沙江路 1518 弄",
          },
          {
            name: "王小虎",
            age: "18",
            sex: "男",
            phone: "13111112222",
            address: "上海市普陀区金沙江路 1518 弄",
          },
          {
            name: "王小虎",
            age: "18",
            sex: "男",
            phone: "13111112222",
            address: "上海市普陀区金沙江路 1518 弄",
          },
        ],
        addData: {
          name: "",
          age: "",
          sex: "",
          phone: "",
          addredd: "",
        },
        editUser: {
          name: "",
          age: "",
          sex: "",
          phone: "",
          addredd: "",
        },
        multipleSelection: [],
        dialogVisible: false,
        dialogFormVisible: false,
        tempData: [],
        select_word: "",
      };
    },
  
    methods: {
      toggleSelection(rows) {
        if (rows) {
          rows.forEach((row) => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
  
      //显示弹出框
      showAddinput() {
        this.dialogVisible = true;
      },
      //编辑
      handleEdit(index, row) {
        console.log(index);
        Object.assign(this.editUser, row);
        this.editUser.index1 = index;
        this.dialogFormVisible = true;
      },
      //提交编辑
      updateuser() {
        this.tableData.splice(this.editUser.index1, 1, this.editUser);
        console.log(this.editUser.index1);
        this.dialogFormVisible = false;
      },
      //删除
      handleDelete(index) {
        this.tableData.splice(index, 1);
      },
      //批量删除
      batchDelete() {},
      //新增
      adduser() {
        this.tableData.push({
          name: this.addData.name,
          age: this.addData.age,
          sex: this.addData.sex,
          phone: this.addData.phone,
          address: this.addData.address,
        });
        this.showAddinput = false;
      },
    },
    watch: {
      select_word: function () {
          this.tempData=this.tableData
          for (const item of this.tempData) {
            if (item.sex.includes(this.select_word)) {
              this.tableData=[]
              this.tableData.push(item)
            }
          }
        
      },
    },
  };
  </script>
  <style scoped>
  .el-header,
  .el-footer {
    background-color: #e9eef3;
    color: #333;
    text-align: center;
    line-height: 60px;
  }
  /* .el-main {
      
      color: #333;
      text-align: center;
      line-height: 160px;
    } */
  .addbtn {
    display: flex;
  }
  .inp {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
  }
  </style>